package baidu.bms.provider;

import baidu.bms.DemoServiceConsumer;
import baidu.bms.DemoServiceProvider;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.support.Parameter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author sunqiyuan
 * @Classname CallDemoService
 * @Description TODO
 * @Date 2020/10/14 10:19 上午
 * @Version V1.0
 */
@RestController("/")
public class HttpService {

    AtomicInteger count = new AtomicInteger(0);
    //  @DubboReference(version = "${demo.service.version}",services = "annotation-provider")
    // check 不在boot启动时检查，该接口是否启动， lazy 懒加载
    @DubboReference(version = "${demo.service.version}", check = false, lazy = true)
    private DemoServiceConsumer demoServiceConsumer;
    @Value("${dubbo.application.name}")
    private String serviceName;

    @GetMapping(path = "methodA")
    public String methodA(@RequestParam("name") String name) {


        if (count.getAndAdd(1) % 2 == 0) {
            // dubbo call http
            String param = String.format(" %s ---> [%s : methodA: httpServer > httpClient] ", name, serviceName);
            RestTemplate call = new RestTemplate();
            return call.getForObject("http://localhost:8081/methodB?name=" + param, String.class);

        } else {
            // dubbo call dubbo
            String param = String.format(" %s ---> [%s : methodA: httpServer > dubboClient] ", name, serviceName);
            return demoServiceConsumer.methodB(param);
        }
    }


}
